package com.fuershiye.woman.monitor.dao;

import java.util.List;

import com.fuershiye.woman.monitor.utils.DaoSqlUtil;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fuershiye.woman.monitor.beans.IdName;
import com.fuershiye.woman.monitor.beans.IdNamePid;
import com.fuershiye.woman.monitor.model.IndicatorType;
import com.google.common.collect.Lists;

@Repository("indicatorTypeDao")
@SuppressWarnings("unchecked")
public class IndicatorTypeDao {

    @Autowired
    private HibernateDao hibernateDao;

    public void save(IndicatorType it){
        hibernateDao.save(it);
    }

    public void delete(Long id) {
        String sql = "update IndicatorType set status = 0 where id = ?";
        SQLQuery query = hibernateDao.getSqlQuery(sql).addEntity(IndicatorType.class);
        query.setLong(0, id);
        query.executeUpdate();
    }

    public IndicatorType get(Long id) {
    	return hibernateDao.getById(IndicatorType.class,id);
    }

    @SuppressWarnings("unchecked")
    public IndicatorType getTop() {

        String hql = "from IndicatorType ORDER BY id DESC";
        Query query = hibernateDao.getQuery(hql);
        query.setFirstResult(0);
        query.setMaxResults(1);
        List<IndicatorType> result = query.list();
        if(result==null||result.size()<1){
        	return null;
        }
        return result.get(0);
    }
    
    @SuppressWarnings("unchecked")
	public List<IndicatorType> list(List<Long> id, String code, Long formsid, String name, Integer status) {

        StringBuffer sb = new StringBuffer("from IndicatorType where 1=1 ");

        sb = DaoSqlUtil.appendSqlListLong(sb, "id", id);
        sb = DaoSqlUtil.appendSql(sb, "code", code);
        sb = DaoSqlUtil.appendSql(sb, "formsid", formsid);
        sb = DaoSqlUtil.appendSql(sb, "name", name);
        sb = DaoSqlUtil.appendSql(sb, "status", status);

        sb.append("order by sort desc");

        Query query = hibernateDao.getQuery(sb.toString());

        query = DaoSqlUtil.appendQueryListLong(query, "id", id);
        query = DaoSqlUtil.appendQuery(query, "code", code);
        query = DaoSqlUtil.appendQuery(query, "formsid", formsid);
        query = DaoSqlUtil.appendQuery(query, "name", name);
        query = DaoSqlUtil.appendQuery(query, "status", status);

        return query.list();

    }

    public List<IdName> listByCondition(String name, String code, Long formsid, List<Long> id, Integer status) {

        StringBuffer sb = new StringBuffer("select id, name from indicator_type where 1=1 ");
        sb = DaoSqlUtil.appendSql(sb, "name", name);
        sb = DaoSqlUtil.appendSql(sb, "code", code);
        sb = DaoSqlUtil.appendSql(sb, "formsid", formsid);
        sb = DaoSqlUtil.appendSqlListLong(sb, "id", id);
        sb = DaoSqlUtil.appendSql(sb, "status", status);
        sb.append("ORDER BY sort DESC");

        Query query = hibernateDao.getSqlQuery(sb.toString());
        query = DaoSqlUtil.appendQuery(query, "name", name);
        query = DaoSqlUtil.appendQuery(query, "code", code);
        query = DaoSqlUtil.appendQuery(query, "formsid", formsid);
        query = DaoSqlUtil.appendQueryListLong(query, "id", id);
        query = DaoSqlUtil.appendQuery(query, "status", status);

        List<Object[]> queryResult = query.list();
        List<IdName> result = Lists.newArrayList();
        for (Object[] fields : queryResult) {
            result.add(turn(fields));
        }
        return result;
    }

    public List<IdName> get(String code, Long formsid, String name, Integer status) {

        StringBuffer sb = new StringBuffer("select id, name from indicator_type where 1=1 ");

        sb = DaoSqlUtil.appendSql(sb, "code", code);
        sb = DaoSqlUtil.appendSql(sb, "formsid", formsid);
        sb = DaoSqlUtil.appendSql(sb, "name", name);
        sb = DaoSqlUtil.appendSql(sb, "status", status);

        sb.append("ORDER BY sort DESC");

        Query query = hibernateDao.getSqlQuery(sb.toString());

        query = DaoSqlUtil.appendQuery(query, "code", code);
        query = DaoSqlUtil.appendQuery(query, "formsid", formsid);
        query = DaoSqlUtil.appendQuery(query, "name", name);
        query = DaoSqlUtil.appendQuery(query, "status", status);

        List<Object[]> queryResult = query.list();

        List<IdName> result = Lists.newArrayList();
        for (Object[] fields : queryResult) {
            result.add(turn(fields));
        }
        return result;

    }

    @SuppressWarnings("unchecked")
    public IndicatorType findBySort(Long pos, Integer aim, Integer count) {
		
    	StringBuffer sb = new StringBuffer("from IndicatorType where sort "); 
		if(aim==1){
			sb.append(">:sort ");
		}else{
			sb.append("<:sort ");
		}
		sb.append("and status =1 order by sort desc");
		
        Query query = hibernateDao.getQuery(sb.toString());
        query.setParameter("sort", pos);
        query.setFirstResult(0);
        query.setMaxResults(count);
        
        List<IndicatorType> result = query.list();
        if(result==null||result.size()<1){
        	return null;
        }
        return result.get(0);

    }

    @SuppressWarnings("unchecked")
    public List<IdName> getAllNames() {

        List<IdName> result = Lists.newArrayList();
        
        String hql = "select id, name from indicator_type where status =1 ORDER BY sort DESC";
        Query query = hibernateDao.getSqlQuery(hql);
        List<Object[]> queryResult = query.list();
        
        for (Object[] fields : queryResult) {
    	  	result.add(turn(fields));
		}
	  	return result;
    }

    private IdName turn (Object[] fields){
        IdName idAndName = new IdName();
        idAndName.setId(fields[0] == null? 0:Long.parseLong(fields[0].toString()));
        idAndName.setName(fields[1] == null? "":fields[1].toString());
        return idAndName;
    }
    
}
